package jdbc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-22
 * Time: 10:29
 * author:UpdataJdbc
 */
public class UpdateJdbc {
    //更新所有信息
    public boolean updateAll(String id,String snid,String name,String author,String press,String isBorrow,String Borrower) throws SQLException {
        //1. 创建DataSource 对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();

        //  构造一个SQL
        String sql1 = "update booklist set booklist.SNID= ?,booklist.name= ?,booklist.author= ?, booklist.press= ?,booklist.isBorrow= ?,booklist.Borrower= ? where booklist.SNID = ?;";//内容待定
        //把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql1);
        statement.setString(1,snid);
        statement.setString(2,name);
        statement.setString(3,author);
        statement.setString(4,press);
        statement.setString(5,isBorrow);
        statement.setString(6,Borrower);
        statement.setString(7,id);
        System.out.println(statement);

        //4.执行SQL
        int ret =  statement.executeUpdate();
        if( ret == 0) {
            return false;
        } else {
            return true;
        }
    }

    //更新借阅信息
    public  boolean lendUpdate(String id,String userid) throws SQLException {
        //1. 创建DataSource 对象
        DataSource dataSource = new MysqlDataSource();
        // 设置数据库所在的地址  向下转型
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        // 设置登录数据库的用户名
        ((MysqlDataSource) dataSource).setUser("root");
        // 设置登录数据库的密码
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2.让代码和数据库建立连接
        //选择java.sql 的Connextion
        Connection connection = dataSource.getConnection();

        //构造一个SQL
        String sql = "update booklist set booklist.isBorrow= ?,booklist.Borrower= ? where booklist.SNID = ?;";//内容待定
        //此处光是一个String 类型的sql还不行,要把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);
        //替换 sql 中 ? 的内容
        statement.setString(1, "是");
        statement.setString(2, userid);
        statement.setString(3, id);

        System.out.println(statement);

        //4.执行SQL
        int ret = statement.executeUpdate();
        System.out.println(ret);

        //5. 此时 SQL 执行完毕, 然后还需要释放资源
        statement.close();
        connection.close();

        if (ret !=0){
            return true;
        }
        else {
            return false;
        }
    }

    //更新归还信息
    public Boolean returnUpdate(String id) throws SQLException {
        //1. 创建DataSource 对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://localhost:3306/libarysystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");
        Connection connection = dataSource.getConnection();
        //  构造一个SQL
        String sql = "update booklist set booklist.isBorrow= ?,booklist.Borrower= ? where booklist.SNID = ?;";//内容待定
        //此处光是一个String 类型的sql还不行,要把这个String 包装成一个"语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, "否");
        statement.setString(2, "无");
        statement.setString(3, id);
        System.out.println(statement);
        //4.执行SQL
        int ret = statement.executeUpdate();
        if( ret !=0) {
            return true;
        } else {
            return false;
        }
    }
}
